﻿using Common;
using Common.DataAnnotations;
using Common.Swagger;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace Sample.Controllers
{
    [ApiController]
    [Route("[controller]")]
    [ApiGroup(ApiGroupNames.数据脱敏)]
    public class SensitiveController : ControllerBase
    {
        /// <summary>
        /// 匿名访问
        /// </summary>
        /// <param name="vo"></param>
        /// <returns></returns>
        [HttpGet("GetByIdForAnonymous")]
        [AllowAnonymous]
        public FuncResult<ExampleDTO> GetByIdForAnonymous([FromQuery] ExampleDTO vo)
        {
            ExampleDTO dto = new ExampleDTO
            {
                ExampleId = vo.ExampleId,
                Array = new long[2] { long.MinValue, long.MaxValue },
                ExampleName = "实例名称",
            };
            return FuncResult.Success(dto);
        }


        [HttpGet]
        public FuncResult<object> GetById([FromQuery] ExampleDTO vo)
        {
            long userId = this.GetUserId();
            //string name = this.GetUserName();
            //var user = factory.GetUserByID(userId);
            var dto = new 
            {
                userId,
                vo.Amount,
                vo.Array,
                vo.ExampleId,
                vo.ExampleName,
                vo.IntValue,
                vo.List,
                vo.OfferType,
            };
            return FuncResult.Success(dto);
        }

        [HttpPost]
        public FuncResult<object> Add([FromBody] ExampleDTO vo)
        {
            return FuncResult.Success(vo);
        }

        [HttpGet("{id}")]
        public FuncResult<object> Get([SensitiveData] long id, [SensitiveData] long queryId)
        {
            //[Common.Web.SensitiveQueryData]
            var model = new
            {
                id = id,
                queryId = queryId
            };
            return FuncResult.Success(model);
        }
    }

    /// <summary>
    /// 实例对象
    /// </summary>
    public class ExampleDTO
    {
        /// <summary>
        /// 实例编号
        /// </summary>
        [SensitiveData]
        public long? ExampleId { get; set; }

        /// <summary>
        /// int类型的脱敏
        /// </summary>
        [SensitiveData]
        public int IntValue { get; set; }
        /// <summary>
        /// 实例名称
        /// </summary>
        public string ExampleName { get; set; }

        /// <summary>
        /// decimal类型的测试
        /// </summary>
        public decimal Amount { get; set; }

        public OfferType OfferType { get; set; }

        /// <summary>
        /// 数组脱敏
        /// </summary>
        [SensitiveData]
        public long[] Array { get; set; }
        [SensitiveData]
        public List<string> List { get; set; }
    }
}
